//
//  YDASearchView.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/27.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDASearchView.h"

@interface YDASearchView()<UISearchBarDelegate>

//搜索栏
@property (nonatomic , weak) UISearchBar *searchBar;

@end

@implementation YDASearchView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = NumberColor(247, 247, 247);
        
        [self searchBar];
        
        
    }
    return self;
}

-(UISearchBar *)searchBar
{
    if (_searchBar == nil) {
        UISearchBar *searchBar = [[UISearchBar alloc] init];
        [self addSubview:searchBar];
        [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.equalTo(@(rate375(358)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(38)));
            }else{
                make.height.equalTo(@(rate750(38)));
            }
        }];
        
        [searchBar setPlaceholder:@"输入股票或名称"];
        [searchBar setTintColor:NumberColor(62, 89, 167)];
        [searchBar setTranslucent:YES];
        [searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];//背景图片
        //        [searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(-5, 0)];// 设置搜索框中文本框的背景的偏移量
        
        [searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中
        [searchBar setShowsSearchResultsButton:NO];// 是否显示搜索结果按钮
        
        [searchBar setKeyboardType:UIKeyboardTypeEmailAddress];
        
        [searchBar setShowsCancelButton:NO];//是否显示取消按钮
        [searchBar setImage:[UIImage imageNamed:@"gpcx-sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//设置搜索图标
        [searchBar sizeToFit];
        
        searchBar.delegate = self;// 设置代理
        
        
        _searchBar = searchBar;
    }
    return _searchBar;
}
/*************UISearchBarDelegate*************/
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //    DDLog(@"点击了搜索====%@",searchBar.text)
    NSString *string = searchBar.text;
    //    searchBar.text = @"";
    [self startSearchWithString:string isStoreHistory:YES];
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //禁止表情输入
    if ([searchBar isFirstResponder]) {
        
        if ([[[searchBar textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[searchBar textInputMode] primaryLanguage]) {
            return NO;
        }
    }
    
    return YES;
}
//当搜索框内容发生变化时调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //    DDLog(@"开始搜索searchText = %@",searchText)
    [self startSearchWithString:searchText isStoreHistory:NO];
}

-(void)startSearchWithString:(NSString *)string isStoreHistory:(BOOL)isHistory
{
    if ([self.delegate respondsToSelector:@selector(aSearchView:searchString:isStoreHistory:)]) {
        [self.delegate aSearchView:self searchString:string isStoreHistory:isHistory];
    }
}
-(void)searhBarBeginFirstResgister
{
    [self.searchBar becomeFirstResponder];
}
-(void)searhBarEndEditor
{
    [self.searchBar endEditing:YES];
}

-(void)searchWithSearchString:(NSString *)searchString
{
    self.searchBar.text = searchString;
    //    [self.searchBar endEditing:YES];
    
    
    [self startSearchWithString:searchString isStoreHistory:NO];
    
}





@end
